//
//  LSZAlbumArtistListViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/5/25.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZAlbumArtistListViewController.h"


@interface LSZAlbumArtistListViewController ()
@property(nonatomic,assign) NSInteger clickCategory;
@end

@implementation LSZAlbumArtistListViewController

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [LCFloatingWindow sharedInstance].hidden = YES;
}
#pragma mark - 得到专辑的数据
-(void)getAlbumData :(NSInteger)page
{
    [LSZRequestResolutionDic getsongList:@"" type:@"1" page:[NSString stringWithFormat:@"%ld",(long)page] fuid:self.fuid :^(BOOL isOk, NSString * string, LSZSongCollectionListManager * mg) {
        if (isOk == YES) {
            if (page == 1) {
                [self.mutArrayAlbum removeAllObjects];
                [self.mutArrayAlbum addObjectsFromArray:mg.arrayData];
                if(mg.arrayData.count == 0){
                    LSZEmptyDataVC * emptyDataVC = [[LSZEmptyDataVC alloc]init];
                    [emptyDataVC showWithView:self.tableViewAlbum];
                    self.tableViewAlbum.mj_footer.hidden = YES;
                }
            }else{
                [self.mutArrayAlbum addObjectsFromArray:mg.arrayData];
                if (mg.arrayData.count == 0) {
                    [self.tableViewAlbum.mj_footer  endRefreshingWithNoMoreData];
                }
            }
            [self.tableViewAlbum reloadData];
        }else{
            if (page == 1) {
                [self getMusicHuanCun:@"1" :string];
            }else{
                [self setStringWarning:string];
            }
        }
    }];
}
#pragma mark - 得到歌手的数据
-(void)getSingerData :(NSInteger)page
{
    [LSZRequestResolutionDic getsongList:@"" type:@"2" page:[NSString stringWithFormat:@"%ld",(long)page] fuid:self.fuid :^(BOOL isOk, NSString * string, LSZSongCollectionListManager * mg) {
        if (isOk == YES) {
            if (page == 1) {
                [self.mutArraySinger removeAllObjects];
                [self.mutArraySinger addObjectsFromArray:mg.arrayData];
                if(mg.arrayData.count == 0){
                    LSZEmptyDataVC * emptyDataVC = [[LSZEmptyDataVC alloc]init];
                    [emptyDataVC showWithView:self.tableViewSinger];
                    self.tableViewSinger.mj_footer.hidden = YES;
                }
            }else{
                [self.mutArraySinger addObjectsFromArray:mg.arrayData];
                if (mg.arrayData.count == 0) {
                    [self.tableViewSinger.mj_footer endRefreshingWithNoMoreData];
                }
            }
            [self.tableViewSinger reloadData];
        }else{
            if (page == 1) {
                [self getMusicHuanCun:@"2" :string];
            }else{
                [self setStringWarning:string];
            }
        }
    }];
}
-(void)getMusicHuanCun :(NSString *)type :(NSString *)string
{
    NSString * str = [NSString stringWithFormat:@"%@_tabbar_collection_%@",shouye_yinpin,type];
    __weak typeof(self) weakSelf = self;
    [LSZCacheHandling initWithfindPlist:str :^(BOOL isOk, NSDictionary * dic) {
        if (isOk == YES) {
            LSZSongCollectionListManager * manager  = [[LSZSongCollectionListManager alloc]initWithSongCollectionList:dic];
            if ([type isEqualToString:@"1"]) {
                [self.mutArrayAlbum removeAllObjects];
                [self.mutArrayAlbum addObjectsFromArray:manager.arrayData];

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [weakSelf.tableViewSinger reloadData];
                }];
            }
            if ([type isEqualToString:@"2"]) {
                [self.mutArraySinger removeAllObjects];
                [self.mutArraySinger addObjectsFromArray:manager.arrayData];
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    [weakSelf.tableViewSinger reloadData];
                }];
            }
        }else{
            [self setStringWarning:string];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收藏详情";

    self.chooseWhere = 2;

    self.mutArrayAlbum = [[NSMutableArray alloc]init];
    self.mutArraySinger= [[NSMutableArray alloc]init];

    [self setAudioUI];
    self.pageAlbum = 1;
    self.pageSinger = 1;
    [self getAlbumData:self.pageAlbum];
    [self getSingerData:self.pageSinger];
    [LCFloatingWindow sharedInstance].hidden = YES;////

    [self.tableViewAlbum registerClass:[LSZAlbumDetailsTableViewCell class] forCellReuseIdentifier:@"LSZAlbumDetailsTableViewCell"];
    [self.tableViewSinger registerClass:[LSZSingerTableViewCell class] forCellReuseIdentifier:@"LSZSingerTableViewCell"];

    NSString * stringZan = [NSString stringWithFormat:@"%@Zan",@"LSZAlbumArtistListViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AlbumArtistListZan) name:stringZan object:nil];//点赞通知

    NSString * strDelete = [NSString stringWithFormat:@"%@DeleteLSZ",@"LSZAlbumArtistListViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(AlbumArtistListDelete) name:strDelete object:nil];//删除通知

    self.tableViewAlbum.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageAlbum = 1;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getAlbumData:self.pageAlbum];
            [self.tableViewAlbum.mj_header endRefreshing];
        });
    }];
    self.tableViewAlbum.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageAlbum++;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //诗歌歌谱的分类
            [self getAlbumData:self.pageAlbum];
            [self.tableViewAlbum.mj_footer endRefreshing];
        });
    }];
    self.tableViewSinger.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入tableViewSinger状态后会自动调用这个block
        self.pageSinger = 1;
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self getSingerData:self.pageSinger];
            [self.tableViewSinger.mj_header endRefreshing];
        });
    }];
    self.tableViewSinger.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        self.pageSinger++;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //诗歌歌谱的分类
            [self getSingerData:self.pageSinger];
            [self.tableViewSinger.mj_footer endRefreshing];
        });
    }];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
//刷新当前的cell数据
-(void)reloadTableView{
     if (_LCPlayer.kstatus == LCMusicPlayerStatusPlaying) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LCPlayStatusStartAnimatingNotification" object:self userInfo:nil];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.tableViewSinger){
        return  self.mutArraySinger.count;
    }
    else{
        return self.mutArrayAlbum.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (ScreenWidth == 320) {
        //5
        return 45;
    }else if(ScreenWidth == 375){
        // 6 7
        return 50;
    }
    else {
        //plus
        return 58;
    }
}
#pragma mark - 更多的操作
-(void)clickMoveOperation:(UIButton *)but
{
    self.clickCategory = but.tag-1;
    LSZPopoverViewController * vc = [[LSZPopoverViewController alloc]init];
     LSZSongCollectionListMg * mg = nil;
    if (self.chooseWhere == 2) {
        mg = self.mutArrayAlbum[self.clickCategory];
        [vc distinguishType:@"1"];
    }else{
        mg = self.mutArraySinger[self.clickCategory];
        [vc distinguishType:@"2"];
    }
    [vc show];
    [vc selectIndexRowWith:4];//布局
    [vc getMusicId:mg.id_id];
    [vc fromWhichInterface:@"LSZAlbumArtistListViewController"];
}
#pragma mark - 点赞
-(void)AlbumArtistListZan
{
    LSZSongCollectionListMg * mg = nil;
    NSString * typeStr = [[NSString alloc]init];
    if (self.chooseWhere == 2) {
        mg = self.mutArrayAlbum[self.clickCategory];
        typeStr = @"1";
    }else{
        mg = self.mutArraySinger[self.clickCategory];
        typeStr = @"2";
    }
    [LSZRequestData audioPointPraise:typeStr :mg.id_id :^(BOOL isOK, NSString * string, NSDictionary * dic) {
        if (isOK == YES) {
            [self setStringWarning:string];
        }else{
            [self setStringWarning:string];
        }
    }];
}
#pragma mark - 删除
-(void)AlbumArtistListDelete
{
    LSZSongCollectionListMg * mg = nil;
    if (self.chooseWhere == 2) {
        mg = self.mutArrayAlbum[self.clickCategory];
    }else{
        mg = self.mutArraySinger[self.clickCategory];
    }
    [LSZRequestData songCollectionDeleted:mg.id_big :^(BOOL isOk, NSString * string, NSDictionary * dic) {
        if (isOk == YES) {
            [self setStringWarning:@"删除成功"];
            if(self.chooseWhere == 2){
                [self.mutArrayAlbum removeObjectAtIndex:self.clickCategory];
                [self.tableViewAlbum reloadData];
            }else{
                [self.mutArraySinger removeObjectAtIndex:self.clickCategory];
                [self.tableViewSinger reloadData];
            }
        }else{
            [self setStringWarning:string];
        }
    }];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (tableView == self.tableViewSinger){
        LSZSingerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZSingerTableViewCell" forIndexPath:indexPath];
       if (indexPath.row >= self.mutArraySinger.count) {
           return cell;
       }
        LSZSongCollectionListMg * mg = nil;
        mg = self.mutArraySinger[indexPath.row];
        [cell.imageViewCover sd_setImageWithURL:[NSURL URLWithString:mg.image] placeholderImage:[UIImage imageNamed:@"我的_设置_关于"]];
       cell.labelTitle.text = mg.title;
       if([(NullStringjudgment(NSStr(self.fuid))) isEqualToString:@""]){
           cell.butMove.hidden = NO;
       }else{
           cell.butMove.hidden = YES;
       }
       cell.butMove.tag = indexPath.row+1;
       [cell.butMove addTarget:self action:@selector(clickMoveOperation:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        LSZAlbumDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZAlbumDetailsTableViewCell" forIndexPath:indexPath];
        if (indexPath.row >= self.mutArrayAlbum.count) {
            return cell;
        }
        LSZSongCollectionListMg * mg = nil;
        mg = self.mutArrayAlbum[indexPath.row];
        [cell.imageViewCover sd_setImageWithURL:[NSURL URLWithString:mg.image] placeholderImage:[UIImage imageNamed:@"我的_设置_关于"]];
        cell.labelTitle.text     = mg.title;
        cell.labelCoverName.text = [NSString stringWithFormat:@"%@  %@首",mg.singerName,mg.total_song];
        //耳机
//        [LSZSpecialPackaging adaptiveWidth:cell.labelCoverName str:cell.labelCoverName.text sizeMaxWidth:ScreenWidth sizeMaxHeight:cell.labelCoverName.frame.size.height];
//        CGRect rect = cell.imageViewHeadset.frame;
//        rect.origin.x = CGRectGetMaxX(cell.labelCoverName.frame)+10;
//        cell.imageViewHeadset.frame = rect;
//        //次数
//        CGRect rectPlays = cell.labelPlaysNumber.frame;
//        rectPlays.origin.x = CGRectGetMaxX(cell.imageViewHeadset.frame)+2;
//        rectPlays.size.width = ScreenWidth-CGRectGetMaxX(cell.imageViewHeadset.frame)-10;
//        cell.labelPlaysNumber.frame = rectPlays;
//        cell.labelPlaysNumber.text = [NSString stringWithFormat:@"%@次",mg.hits];
        if([(NullStringjudgment(NSStr(self.fuid))) isEqualToString:@""]){
            cell.butMove.hidden = NO;
        }else{
            cell.butMove.hidden = YES;
        }
        cell.butMove.tag = indexPath.row+1;
        [cell.butMove addTarget:self action:@selector(clickMoveOperation:) forControlEvents:UIControlEventTouchUpInside];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
#pragma mark - 切换歌
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSZNewAudioPlayImageAndTableViewVC * vc = [[LSZNewAudioPlayImageAndTableViewVC alloc]init];
     LSZSongCollectionListMg * audioMg = nil;
    if (tableView == self.tableViewSinger) {
        audioMg = self.mutArraySinger[indexPath.row];
    }
    else{
        audioMg = self.mutArrayAlbum[indexPath.row];
    }
    [LCUtils collectionAlbumArtistDetailsInterfaceJump:vc audioMg:audioMg typeComeWhere:@"1"];
    vc.lastPageNumberRow = [NSString stringWithFormat:@"%ld",(long)indexPath.row];
    vc.lastSection = [NSString stringWithFormat:@"%ld",(long)indexPath.section-1];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setAudioUI
{
    self.viewTop = [[UIView alloc]initWithFrame:CGRectMake(0,NAVIGATION_BAR_HEIGHT, ScreenWidth,50)];
    self.viewTop.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewTop];

    UIButton * butAlbum = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2,self.viewTop.frame.size.height)];
    [butAlbum setTitle:@"专 辑" forState:UIControlStateNormal];
    [butAlbum setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [butAlbum addTarget:self action:@selector(clickAlbum) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butAlbum];

    UIButton * butSinger = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/2, 0, ScreenWidth/2,self.viewTop.frame.size.height)];
    [butSinger setTitle:@"歌手" forState:UIControlStateNormal];
    [butSinger setTitleColor:titleColorLSZ forState:UIControlStateNormal];
    [butSinger addTarget:self action:@selector(clickSinger) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTop addSubview:butSinger];

    UIView * viewWhite = [[UIView alloc]initWithFrame:CGRectMake(0,self.viewTop.frame.size.height-0.5, ScreenWidth, 0.5)];
    viewWhite.backgroundColor = divider;
    [self.viewTop addSubview:viewWhite];

    UIView * viewVerticalBar = [[UIView alloc]initWithFrame:CGRectMake((self.viewTop.frame.size.width-0.5)/2, 10 , 0.5, self.viewTop.frame.size.height-20)];
    viewVerticalBar.backgroundColor = divider;
    [self.viewTop addSubview:viewVerticalBar];

    self.viewRed = [[UIView alloc]initWithFrame:CGRectMake(self.viewTop.frame.size.width/6, self.viewTop.frame.size.height-1,self.viewTop.frame.size.width/6, 1)];
    self.viewRed.backgroundColor = redUIColor;
    [self.viewTop addSubview:self.viewRed];

    self.scrollViewBack = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewTop.frame), ScreenWidth, ScreenHeight-CGRectGetMaxY(self.viewTop.frame)-HOME_INDICATOR_HEIGHT)];
    self.scrollViewBack.delegate = self;
    self.scrollViewBack.bounces = NO; // 默认为YES 取消设置NO
    // 当bounces属性为NO时，以下两个属性设置无效
    self.scrollViewBack.alwaysBounceHorizontal = YES; // 水平方向弹簧效果
    self.scrollViewBack.alwaysBounceVertical = YES; // 垂直方向弹簧效果
    self.scrollViewBack.decelerationRate = 0.1; // 值域 0~1 值越小减速停止的时间越短
    self.scrollViewBack.pagingEnabled = YES; // 默认为NO，设置YES内容视图整页滚动
    self.scrollViewBack.showsVerticalScrollIndicator = YES; // 垂直方向
    self.scrollViewBack.contentSize = CGSizeMake(ScreenWidth*2, ScreenHeight-ScreenHeight-50);
    [self.view addSubview:self.scrollViewBack];

    self.tableViewAlbum = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth,self.scrollViewBack.frame.size.height) style:UITableViewStylePlain];
    self.tableViewAlbum.delegate = self;
    self.tableViewAlbum.dataSource = self;
    self.tableViewAlbum.estimatedRowHeight =0;
    self.tableViewAlbum.estimatedSectionFooterHeight =0;
    self.tableViewAlbum.estimatedSectionHeaderHeight =0;
    self.tableViewAlbum.separatorColor = divider;
    self.tableViewAlbum.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.tableViewAlbum.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.scrollViewBack addSubview:self.tableViewAlbum];

    self.tableViewSinger = [[UITableView alloc] initWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth,self.scrollViewBack.frame.size.height) style:UITableViewStylePlain];
    self.tableViewSinger.delegate = self;
    self.tableViewSinger.dataSource = self;
    self.tableViewSinger.estimatedRowHeight =0;
    self.tableViewSinger.estimatedSectionFooterHeight =0;
    self.tableViewSinger.estimatedSectionHeaderHeight =0;
    self.tableViewSinger.separatorColor = divider;
    self.tableViewSinger.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
    self.tableViewSinger.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.scrollViewBack addSubview:self.tableViewSinger];
}
#pragma mark - 点击专辑
-(void)clickAlbum
{
    [self.scrollViewBack setContentOffset:CGPointMake(0, 0) animated:YES];
    [self MoveAlbumRedView];
}
-(void)MoveAlbumRedView
{
    self.chooseWhere = 2;
    [LCFloatingWindow sharedInstance].hidden = YES;
    self.viewRed.frame = CGRectMake(self.viewTop.frame.size.width/6,self.viewTop.frame.size.height-1,self.viewTop.frame.size.width/6, 1);
}
#pragma mark - 点击歌手
-(void)clickSinger
{
    [self.scrollViewBack setContentOffset:CGPointMake(ScreenWidth, 0) animated:YES];
    [self MoveSingerView];
}
-(void)MoveSingerView
{
    self.chooseWhere = 3;
    [LCFloatingWindow sharedInstance].hidden = YES;
    self.viewRed.frame = CGRectMake(self.viewTop.frame.size.width/6*4,self.viewTop.frame.size.height-1,self.viewTop.frame.size.width/6, 1);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == self.tableViewAlbum||scrollView == self.tableViewSinger) {
        return ;
    }
    NSInteger index = scrollView.contentOffset.x / self.view.bounds.size.width;
    if (index == 0) {
        [self MoveAlbumRedView];
    }
    else{
        [self MoveSingerView];
    }
}
@end
